package main

import (
	"cross-shard-channel/user"
	"fmt"
	"time"
)

func main() {
	// 对每次连接需要创建一个client
	user.UN.Init()

	// 测试数据集合
	advIP := "http://localhost:8000"
	ourb := 10
	advb := 10
	life := 1000

	t1 := time.Now() // 计录开始时间
	//进行100次通道创建计算平均值
	user.OpenChannel(advIP, 0, ourb, advb, life)
	for i := 0; i < 10; i++ {

		for j := 0; j < 25; j++ {
			user.UpdateChannel(0, 1)
			user.UpdateChannel(0, -1)
		}

	}
	user.CloseChannel(0)
	t2 := time.Now() // 记录结束时间
	d := t2.Sub(t1)  //两个时间相减

	fmt.Println("总执行时间=", d)
	fmt.Println("单次通道创建执行时间=", d/500)

	/*
		// 执行通道更新的次数
		times := 2
		for i := 0; i < times/2; i++ {
			user.UpdateChannel(1, 1)
			user.UpdateChannel(1, -1)
		}

		user.CloseChannel(cid)
		ch := user.UN.GetChannelFromNode(cid)
		if ch == nil {
			println("channel create fail")
		} else {
			// ch.Show()

		}
	*/
}

/*
type User struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
}

func main() {
	resp, _ := http.Get("http://localhost:8080/?a=123456&b=aaa&b=bbb")
	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)
	fmt.Println("first respond: ", string(body))

	var user User
	user.Name = "aaa"
	user.Age = 99
	if bs, err := json.Marshal(user); err == nil {
		//        fmt.Println(string(bs))
		req := bytes.NewBuffer([]byte(bs))
		// tmp := `{"name":"junneyang", "age": 88}`
		// req = bytes.NewBuffer([]byte(tmp))

		body_type := "application/json;charset=utf-8"
		resp, _ = http.Post("http://localhost:8080/test/", body_type, req)
		body, _ = ioutil.ReadAll(resp.Body)
		fmt.Println(string(body))
	} else {
		fmt.Println(err)
	}

	client := &http.Client{}
	request, _ := http.NewRequest("GET", "http://localhost:8080/?a=123456&b=aaa&b=bbb", nil)
	request.Header.Set("Connection", "keep-alive")
	response, _ := client.Do(request)
	if response.StatusCode == 200 {
		body, _ := ioutil.ReadAll(response.Body)
		fmt.Println(string(body))
	}

	req := `{"name":"junneyang", "age": 88}`
	req_new := bytes.NewBuffer([]byte(req))
	request, _ = http.NewRequest("POST", "http://localhost:8080/test/", req_new)
	request.Header.Set("Content-type", "application/json")
	response, _ = client.Do(request)
	if response.StatusCode == 200 {
		body, _ := ioutil.ReadAll(response.Body)
		fmt.Println(string(body))
	}
}

*/
